package com.chenglu.android.common.base;

import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;


import com.chenglu.android.R;
import com.chenglu.android.common.widget.LoadingDialog;

import autodispose2.AutoDispose;
import autodispose2.AutoDisposeConverter;
import autodispose2.androidx.lifecycle.AndroidLifecycleScopeProvider;

/**
 * @author lyj
 * @Created by Copyright © 2023 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：mvp基类
 */
public abstract class BaseMvpActivity<T extends BasePresenter> extends BaseActivity implements BaseView {

    protected T mPresenter;


    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //沉浸式状态栏
//        setImmersionBarColor(R.color.white);

        loadingDialog = new LoadingDialog(mContext);
        loadingDialog.setCancelable(loadingCancelable());
        //创建present
//        presenter = createPresenter();
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
    }


    @Override
    protected void onDestroy() {
        if (mPresenter != null) {
            mPresenter.detachView();
            mPresenter = null;
        }


        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }

        super.onDestroy();
    }

//    /**
//     * 返回Presenter
//     *
//     * @return T
//     */
//    protected T getPresenter() {
//        return presenter;
//    }
//
//    /**
//     * 创建Presenter
//     *
//     * @return Presenter
//     */
//    protected abstract T createPresenter();


    //***************************************IBaseView方法实现 start*************************************

    @Override
    public void showLoading() {
        showLoading("");
    }

    /**
     * 显示加载框
     *
     * @param msg 加载框文字
     */
    public void showLoading(String msg) {
        if (loadingDialog != null && !loadingDialog.isShowing()) {
            if (!TextUtils.isEmpty(msg)) {
                loadingDialog.setTitleText(msg);
            }
            loadingDialog.show();
        }
//        hideEmptyView();
    }

    @Override
    public void dismissLoading() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }


    /**
     * 加载框是否可以取消
     *
     * @return boolean
     */
    protected boolean loadingCancelable() {
        return true;
    }


    @Override
    public void onError(String errorMsg) {
        dismissLoading();
    }


    @Override
    public void onEmpty(Object tag) {
        dismissLoading();
    }

    @Override
    public void onNetWorkError(Object tag) {

    }

    @Override
    public void onNetOutTimeError(String tag) {

    }

    /**
     * 绑定生命周期 防止MVP内存泄漏
     *
     * @param <T>
     * @return
     */
    @Override
    public <T> AutoDisposeConverter<T> bindAutoDispose() {
        return AutoDispose.autoDisposable(AndroidLifecycleScopeProvider
                .from(this, Lifecycle.Event.ON_DESTROY));
    }
}
